<template>
  <div class="checkout-page">
    <div class="page-header">
      <h1>订单结算</h1>
    </div>

    <div class="checkout-container">
      <div class="order-details">
        <div class="section-card">
          <h2>订单信息</h2>
          <div class="order-items">
            <div v-for="item in cartItems" :key="item.id" class="order-item">
              <img :src="item.image" :alt="item.name">
              <div class="item-info">
                <h3>{{ item.name }}</h3>
                <p class="quantity">数量: {{ item.quantity }}</p>
                <p class="price">¥{{ (item.price * item.quantity).toFixed(2) }}</p>
              </div>
            </div>
          </div>
        </div>
        <!-- 收货信息卡片 -->
        <div class="section-card">
          <h2>收货信息</h2>
          <div class="form-group">
            <label>收货人姓名</label>
            <input v-model="shippingInfo.name" type="text" placeholder="请输入姓名">
          </div>
          <div class="form-group">
            <label>手机号码</label>
            <input v-model="shippingInfo.phone" type="tel" placeholder="请输入手机号">
          </div>
          <div class="form-group">
            <label>收货地址</label>
            <textarea v-model="shippingInfo.address" placeholder="请输入详细地址"></textarea>
          </div>
          <div class="form-group">
            <label>备注</label>
            <textarea v-model="shippingInfo.note" placeholder="有什么想告诉我们的吗？"></textarea>
          </div>
        </div>
      </div>
      <!-- 支付摘要 -->
      <div class="payment-summary">
        <div class="section-card">
          <h2>支付方式</h2>
          <div class="payment-methods">
            <div 
              v-for="method in paymentMethods" 
              :key="method.id"
              :class="['payment-method', selectedPayment === method.id ? 'selected' : '']"
              @click="selectedPayment = method.id"
            >
              <i :class="method.icon"></i>
              <span>{{ method.name }}</span>
            </div>
          </div>
        </div>

        <div class="section-card order-summary">
          <h2>订单摘要</h2>
          <div class="summary-row">
            <span>商品总额</span>
            <span>¥{{ subtotal.toFixed(2) }}</span>
          </div>
          <div class="summary-row">
            <span>运费</span>
            <span>¥{{ shipping.toFixed(2) }}</span>
          </div>
          <div class="summary-row total">
            <span>应付总额</span>
            <span>¥{{ total.toFixed(2) }}</span>
          </div>
          <button @click="submitOrder" class="submit-btn" :disabled="!canSubmit">
            <i class="fas fa-lock"></i>
            提交订单
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'Checkout',
  setup() {
    // 购物车商品列表
    const router = useRouter()
    // 收货信息
    const cartItems = ref([])
    const shipping = ref(0)
    const selectedPayment = ref('')
    const shippingInfo = ref({
      name: '',
      phone: '',
      address: '',
      note: ''
    })
    // 支付方式列表
    const paymentMethods = [
      { id: 'alipay', name: '支付宝', icon: 'fab fa-alipay' },
      { id: 'wechat', name: '微信支付', icon: 'fab fa-weixin' },
      { id: 'card', name: '银行卡', icon: 'fas fa-credit-card' }
    ]
    // 加载购物车数据
    const loadCart = () => {
      const savedCart = localStorage.getItem('cart')
      if (!savedCart) {
        router.push('/cart')
        return
      }
      cartItems.value = JSON.parse(savedCart)
      updateShipping()
    }

    const updateShipping = () => {
      shipping.value = subtotal.value >= 199 ? 0 : 20
    }
    // 计算商品总价
    const subtotal = computed(() => {
      return cartItems.value.reduce((total, item) => {
        return total + (item.price * item.quantity)
      }, 0)
    })
     // 计算总计
    const total = computed(() => {
      return subtotal.value + shipping.value
    })
    // 判断是否可以提交订单
    const canSubmit = computed(() => {
      return shippingInfo.value.name &&
             shippingInfo.value.phone &&
             shippingInfo.value.address &&
             selectedPayment.value
    })
    // 提交订单的方法
    const submitOrder = () => {
      if (!canSubmit.value) return

      const order = {
        items: cartItems.value,
        shipping: shippingInfo.value,
        payment: selectedPayment.value,
        total: total.value,
        date: new Date().toISOString()
      }

      // 这里可以添加向后端发送订单的逻辑
      console.log('提交订单:', order)

      // 清空购物车
      localStorage.removeItem('cart')
      window.dispatchEvent(new Event('storage'))

      // 显示成功提示
      alert('订单提交成功！')
      router.push('/')
    }

    onMounted(() => {
      loadCart()
    })

    return {
      cartItems,
      shipping,
      subtotal,
      total,
      paymentMethods,
      selectedPayment,
      shippingInfo,
      canSubmit,
      submitOrder
    }
  }
}
</script>

<style scoped>
.checkout-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-header h1 {
  font-size: 2.5em;
  color: #B784B7;
  margin-bottom: 10px;
}

.checkout-container {
  display: grid; /* 结算容器布局 */
  grid-template-columns: 1fr 400px;
  gap: 30px;
}

.section-card {
  background: white;
  border-radius: 15px;
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.05);
  padding: 25px;
  margin-bottom: 20px;
}

.section-card h2 {
  color: #B784B7;
  font-size: 1.5em;
  margin-bottom: 20px;
}

.order-items {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.order-item {
  display: flex;
  gap: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.order-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.order-item img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 10px;
}

.item-info {
  flex: 1;
}

.item-info h3 {
  color: #333;
  margin: 0 0 5px 0;
  font-size: 1.1em;
}

.item-info .quantity {
  color: #666;
  font-size: 0.9em;
}
  /* 商品小计颜色 */
.item-info .price {
  color: #B784B7;
  font-weight: bold;
  margin-top: 5px;
}

.form-group {
  margin-bottom: 20px;/* 表单组底部外边距 */
}

.form-group label {
  /* 表单标签显示方式 */
  display: block;
  margin-bottom: 8px;
  color: #666;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #D4B8D4;
  border-radius: 8px;
  font-size: 1em;
  transition: border-color 0.3s ease;
}

.form-group textarea {
  height: 100px;
  resize: vertical;
}

.form-group input:focus,
.form-group textarea:focus {
  outline: none;/* 表单输入框和文本域聚焦时无边框 */
  border-color: #B784B7;
}

.payment-methods {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 15px;
}

.payment-method {
  display: flex; /* 支付方式列表布局 */
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 15px;
  border: 2px solid #D4B8D4;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.payment-method i {
  font-size: 24px;
  color: #B784B7; /* 支付方式图标颜色 */
}

.payment-method.selected {
  background: #F8F1F8;
  border-color: #B784B7;
}

.payment-method:hover {
  transform: translateY(-2px); /* 支付方式悬停时向上移动 */
  box-shadow: 0 2px 10px rgba(183, 132, 183, 0.1);
}

.order-summary {
  position: sticky;/* 订单摘要粘性定位 */
  top: 100px;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  color: #666;
}

.summary-row.total {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 2px solid #F8F1F8;
  font-size: 1.2em;
  font-weight: bold;
  color: #B784B7;
}

.submit-btn {
  width: 100%;
  padding: 15px;
  background: #B784B7;
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 1.1em;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
}

.submit-btn:hover:not(:disabled) {
  background: #9B6B9B;/* 提交订单按钮悬停时的背景颜色 */
  transform: translateY(-2px);
}

.submit-btn:disabled {
  background: #D4B8D4;
  cursor: not-allowed;
}

@media (max-width: 900px) {
  .checkout-container {
    /* 小屏幕下结算容器列模板 */
    grid-template-columns: 1fr;
  }

  .order-summary {
    /* 小屏幕下订单摘要取消粘性定位 */
    position: static;
  }
}

@media (max-width: 600px) {
  .order-item {
    flex-direction: column; /* 小屏幕下订单商品列模板 */
    text-align: center;
  }

  .order-item img {
    margin: 0 auto;
  }

  .payment-methods {
    grid-template-columns: 1fr; /* 小屏幕下支付方式列表列模板 */
  }
}
</style>